﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.Data.SQLite;  

namespace WebService
{
    /// <summary>
    /// GetItemInfo 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class GetItemInfo : System.Web.Services.WebService
    {

        [WebMethod]
        public List<CLS.ClsItem> GetItem()
        {
            return GetAllItem();
        }


        private List<CLS.ClsItem> GetAllItem()
        {
            List<CLS.ClsItem> list = new List<CLS.ClsItem>();
            CLS.ClsItem item = null;

            string strDirectory = Server.MapPath("/");
           string connStr = SQLiteConnectionString.GetConnectionString(strDirectory+"DB.db");

            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                conn.Open();
                SQLiteCommand comm = conn.CreateCommand();
                comm.CommandText = "select * from ItemMaster";
              
                using (SQLiteDataReader reader = comm.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        item = new CLS.ClsItem();
                        item.ItemID = reader["ItemID"].ToString();
                        item.ItemName = reader["ItemName"].ToString();
                        item.ItemGrede = reader["ItemGrade"].ToString();
                        item.ItemUOM = reader["ItemUOM"].ToString();
                        item.ItemProduct = reader["ItemProduct"].ToString();
                        item.ItemSupplier = reader["ItemSupplier"].ToString();
                        list.Add(item);
                    }
                }
                conn.Close();
            }
            return list;
          

        }

       
    }

    /// <summary>  

    /// 生成SQLite连接字符串  

    /// </summary>  

    public static class SQLiteConnectionString
    {

        public static string GetConnectionString(string path)
        {
            return GetConnectionString(path, null);
        }

        public static string GetConnectionString(string path, string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                return "Data Source=" + path;
            }
            else
            {
                return "Data Source=" + path + ";Password=" + password;
            }
        }

    }  
}
